//
//  SelfViewController.m
//  SugarPile
//
//  Created by Charlie. on 16/1/18.
//  Copyright © 2016年 www.lanou39.蓝鸥科技. All rights reserved.
//

#import "SelfViewController.h"
#import "ClearData.h"
#import "AppDelegate.h"
#import "YJViewController.h"
@interface SelfViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView; /**<tableView*/
@property (nonatomic, assign) BOOL isLogin; /**< 定义全局bool值 */
@property (nonatomic, retain)UIView *backView;
@property (nonatomic, retain)UISwitch *seitch;
@end

@implementation SelfViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // tableView
    [self createCollection];
    AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    self.backView  = [[ UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.backView.backgroundColor = [UIColor blackColor];
    self.backView.alpha = 0.5;
    self.backView.userInteractionEnabled = NO;
    [delegate.window addSubview:_backView];
    _backView.hidden = YES;
    [_backView release];
}
- (void)createCollection
{
    // 初始化话
    self.isLogin = NO;
    
    // 创建 tableView
    [self createtableView];
}
// tableView
- (void)createtableView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    // 协议
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [self.tableView release];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
}
// table个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    // 判断是否为第0位元素
    if (indexPath.row == 0) {
         NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
        CGFloat h = [ClearData folderSizeAtPath:path];
        cell.textLabel.text = [NSString stringWithFormat:@"清除缓存       %.2fMB", h];
        cell.tag = 10000;
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    if (indexPath.row == 1) {
    
        cell.textLabel.text = @"夜间模式";
            self.seitch = [[UISwitch alloc]init];
            cell.accessoryView = _seitch;
            
            [self.seitch addTarget:self action:@selector(swich:) forControlEvents:UIControlEventValueChanged];
            [_seitch release];
        
    }
    if (indexPath.row == 2) {
        cell.textLabel.text = @"意见反馈";
    }
    return cell;
}
/**<  按钮方法中心 */
-(void)swich:(UISwitch *)button
{
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:button.on forKey:@"isNight"];
    
    if (button.on) {
        self.backView.hidden = NO;
    }else
    {
        self.backView.hidden = YES;
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
        // 提示控制器
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"清空缓存么 亲" message:@"清除缓存会导致下次运行程序的时候拖慢 确认删除么" preferredStyle:UIAlertControllerStyleAlert];
        
        
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"点击确认");
            [ClearData clearCache:path];
            UITableViewCell *cell = [self.view viewWithTag:10000];
            CGFloat h = [ClearData folderSizeAtPath:path];
            cell.textLabel.text = [NSString stringWithFormat:@"清除缓存       %.2fMB", h];
        }]];
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }]];
       
        [self presentViewController:alertController animated:YES completion:nil];

    }
    if (indexPath.row == 2) {
        YJViewController *YJVC = [[[YJViewController alloc] init] autorelease];
        UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:YJVC];
    [self presentViewController:nc animated:YES completion:^{
            
        }];
    }
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    BOOL isNight = [defaults boolForKey:@"isNight"];
    if (isNight) {
        _seitch.on = YES;
        //         [[NSNotificationCenter defaultCenter] postNotificationName:@"nightCome" object:nil userInfo:nil];
        
    }else{
        _seitch.on = NO;
        //        [[NSNotificationCenter defaultCenter] postNotificationName:@"dayCome" object:nil userInfo:nil];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
